

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Han Qiu">
  <meta name="keywords" content="">
  
    <meta name="description" content="一、计算属性 computed1、计算属性的 setter 和 getter 每个计算属性都包含一个 setter 和一个 getter 计算属性一般没有 set 方法，只读属性 计算属性在使用时作为一个属性，不用加() 注意：如果计算属性使用了箭头函数，则 this 不会指向这个组件的实例，不过仍然可以将其实例作为函数的第一个参数来访问  1234567891011121314151617181">
<meta property="og:type" content="article">
<meta property="og:title" content="vue计算属性computed和侦听器watch">
<meta property="og:url" content="http://example.com/2022/01/06/vue%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7computed%E5%92%8C%E4%BE%A6%E5%90%AC%E5%99%A8watch/index.html">
<meta property="og:site_name" content="寒秋の博客">
<meta property="og:description" content="一、计算属性 computed1、计算属性的 setter 和 getter 每个计算属性都包含一个 setter 和一个 getter 计算属性一般没有 set 方法，只读属性 计算属性在使用时作为一个属性，不用加() 注意：如果计算属性使用了箭头函数，则 this 不会指向这个组件的实例，不过仍然可以将其实例作为函数的第一个参数来访问  1234567891011121314151617181">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-01-06T03:39:37.000Z">
<meta property="article:modified_time" content="2022-03-09T09:59:07.606Z">
<meta property="article:author" content="Han Qiu">
<meta property="article:tag" content="vue">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>vue计算属性computed和侦听器watch - 寒秋の博客</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>blogs</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="vue计算属性computed和侦听器watch">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-01-06 11:39" pubdate>
        2022年1月6日 中午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.3k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      12 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">vue计算属性computed和侦听器watch</h1>
            
            <div class="markdown-body">
              <h3 id="一、计算属性-computed"><a href="#一、计算属性-computed" class="headerlink" title="一、计算属性 computed"></a>一、计算属性 computed</h3><h4 id="1、计算属性的-setter-和-getter"><a href="#1、计算属性的-setter-和-getter" class="headerlink" title="1、计算属性的 setter 和 getter"></a>1、计算属性的 setter 和 getter</h4><ul>
<li>每个计算属性都包含一个 setter 和一个 getter</li>
<li>计算属性一般没有 set 方法，只读属性</li>
<li>计算属性在使用时作为一个属性，不用加()</li>
<li>注意：如果计算属性使用了箭头函数，则 this 不会指向这个组件的实例，不过仍然可以将其实例作为函数的第一个参数来访问</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></div></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> vm = <span class="hljs-keyword">new</span> Vue(&#123;<br>    <span class="hljs-attr">data</span>: &#123; <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> &#125;,<br>    <span class="hljs-attr">computed</span>: &#123;<br>        <span class="hljs-comment">// 仅读取</span><br>        <span class="hljs-attr">aDouble</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123; <br>            <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.a * <span class="hljs-number">2</span> <br>        &#125;, <br>        <span class="hljs-comment">// 读取和设置 </span><br>        <span class="hljs-attr">aPlus</span>: &#123; <br>            <span class="hljs-attr">get</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>                <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>.a + <span class="hljs-number">1</span><br>            &#125;, <br>            <span class="hljs-attr">set</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">v</span>) </span>&#123; <br>                <span class="hljs-built_in">this</span>.a = v - <span class="hljs-number">1</span> <br>            &#125;<br>        &#125; <br>    &#125;<br>&#125;)<br>vm.aPlus <span class="hljs-comment">// =&gt; 2</span><br>vm.aPlus = <span class="hljs-number">3</span> <br>vm.a <span class="hljs-comment">// =&gt; 2 </span><br>vm.aDouble <span class="hljs-comment">// =&gt; 4</span><br></code></pre></td></tr></table></figure>

<h4 id="2、计算属性的缓存"><a href="#2、计算属性的缓存" class="headerlink" title="2、计算属性的缓存"></a>2、计算属性的缓存</h4><ul>
<li>计算属性的结果会被缓存，除非依赖的响应式 property 变化才会重新计算</li>
<li>注意：如果某个依赖 (比如非响应式 property) 在该实例范畴之外，则计算属性是不会被更新的</li>
</ul>
<h3 id="二、侦听器-watch"><a href="#二、侦听器-watch" class="headerlink" title="二、侦听器 watch"></a>二、侦听器 watch</h3><ul>
<li>watch 为一个对象，键是需要观察的表达式，值为对应的回调函数。值也可以是方法名，或者包含选项的对象</li>
<li>注意：不能使用箭头函数来定义 watcher 函数，this 指向会有问题</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> vm = <span class="hljs-keyword">new</span> Vue(&#123;<br>    <span class="hljs-attr">data</span>: &#123; <br>        <span class="hljs-attr">a</span>: <span class="hljs-number">1</span>,<br>        <span class="hljs-attr">b</span>: <span class="hljs-number">2</span>,<br>        <span class="hljs-attr">c</span>: &#123;<br>            <span class="hljs-attr">d</span>: <span class="hljs-number">3</span><br>        &#125;<br>    &#125;,<br>    <span class="hljs-attr">watch</span>: &#123;<br>        <span class="hljs-comment">// 值为一个函数</span><br>        <span class="hljs-attr">a</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">val, oldVal</span>) </span>&#123;<br>            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`newVal: <span class="hljs-subst">$&#123;val&#125;</span>, oldVal: <span class="hljs-subst">$&#123;oldVal&#125;</span>`</span>)<br>        &#125;,<br>        <span class="hljs-comment">// 值可以为一个方法名</span><br>        <span class="hljs-attr">b</span>: <span class="hljs-string">&#x27;someMethod&#x27;</span>,<br>        <span class="hljs-comment">// 值为一个对象，可以配置 deep 和 immediate 属性值</span><br>        <span class="hljs-attr">c</span>: &#123;<br>            <span class="hljs-attr">handler</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">val, oldVal</span>) </span>&#123;<br>                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`newVal: <span class="hljs-subst">$&#123;val&#125;</span>, oldVal: <span class="hljs-subst">$&#123;oldVal&#125;</span>`</span>)<br>            &#125;,<br>            <span class="hljs-comment">// 深度监听，回调会在被监听对象任何属性改变时调用，不论嵌套多深</span><br>            <span class="hljs-attr">deep</span>: <span class="hljs-literal">true</span>,<br>            <span class="hljs-comment">// 立即监听，回调会在监听开始时就调用一次。否则回调只在数据变化时调用</span><br>            <span class="hljs-attr">immediate</span>: <span class="hljs-literal">true</span><br>        &#125;,<br>        <span class="hljs-comment">// 用于监听 c.d 的值变化</span><br>        <span class="hljs-string">&#x27;c.d&#x27;</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">val, oldVal</span>) </span>&#123;<br>            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`newVal: <span class="hljs-subst">$&#123;val&#125;</span>, oldVal: <span class="hljs-subst">$&#123;oldVal&#125;</span>`</span>)<br>        &#125;<br>    &#125;<br>&#125;)<br></code></pre></td></tr></table></figure>

<blockquote>
<p>参考文档：<br><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#watch">vue2 官方 API 选项 computed 和 watch</a></p>
</blockquote>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/vue/">vue</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/vue/">vue</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/01/06/canvas%E5%AE%9E%E7%8E%B0%E7%AE%80%E6%98%93%E7%AD%BE%E5%AD%97%E6%9D%BF/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">canvas实现简易签字板</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/01/06/vue%E5%B8%B8%E7%94%A8%E6%8C%87%E4%BB%A4/">
                        <span class="hidden-mobile">vue常用指令</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
